<template>
  <div>
    <el-dialog :title="'库存明细'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="80%">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="8">
            <el-form-item label="日期">
              <el-date-picker v-model="date" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd" @change="handleDate" clearable> </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="织物类型">
              <el-select v-model="ruleForm.type" placeholder="请选择织物类型" clearable>
                <el-option v-for="item in typeArray" :key="item.key" :label="item.value" :value="item.key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" @click="getDetail">查询</el-button>
              <el-button @click="resetForm2">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table :data="tableData" :borderr="true" :header-cell-style="tabHeadStyle" :cell-style="tabcellStyle" :row-style="rowStyle" height="500">
              <el-table-column prop="create_time" label="时间" align="center"> </el-table-column>
              <el-table-column v-for="(item, index) in tableFields" :key="index" :label="item.value" align="center">
                <template>
                  <el-table-column v-for="(unit, unitIndex) in item.child" :key="unitIndex" :prop="unit.key" :label="unit.value" align="center" min-width="90"> </el-table-column>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">关 闭</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { repairClothInventoryInfo, getInventoryField, saveChild } from "@/api/stock";
import { getClothType } from "@/api/system";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    startdate: {
      type: String,
      default: "",
    },
    areaName: {
      type: String,
      default: "",
    },
    enddate: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 表头样式
      tabHeadStyle: {
        "background-color": "#f5f7fa",
        color: "#303133",
        "font-weight": "bold",
        "text-align": "center",
      },
      // 行样式
      tabcellStyle: {
        color: "#666",
      },
      rowStyle: {
        height: "20px",
      },
      ruleForm: {
        // 表单字段
        type: "",
        startdate: "",
        enddate: "",
      },
      rules: {
        // 表单规则
        name: [{ required: true, message: "请输入院区名称", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      tableData: [],
      tableFields: [],
      date: [],
      typeArray: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          // this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        this.getInventoryField();

        this.getClothTypeList();
      }
    },
  },
  methods: {
    // 重置
    resetForm2() {
      this.ruleForm = {
        type: "",
        startdate: "",
        enddate: "",
      };

      this.getDetail();
    },
    // 详情
    getDetail() {
      repairClothInventoryInfo({ type: this.ruleForm.type,startdate:this.ruleForm.startdate,enddate:this.ruleForm.enddate }).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveChild(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 获取设备类型列表
    getClothTypeList() {
      getClothType().then((res) => {
        if (res.code == 200) {
          this.typeArray = res.data;
        }
      });
    },
    // 获取表头字段
    getInventoryField() {
      getInventoryField().then((res) => {
        if (res.code == 200) {
          this.tableFields = res.data;
        }
      });
    },
    // 日期切换
    handleDate() {
      if (this.date && this.date.length == 2) {
        this.ruleForm.startdate = this.date[0];
        this.ruleForm.enddate = this.date[1];
      } else {
        this.ruleForm.startdate = "";
        this.ruleForm.enddate = "";
      }

      this.getDetail();
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}
</style>
